﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using BookStore.Data;
using BookStore.Bussiness;


namespace BookStore.admin
{
    public partial class Author : System.Web.UI.Page
    {
        static bool flag = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Convert.ToString(Session["Administrator"])))
            {
                Response.Redirect("~/admin/Login.aspx");
            }
            showAll();
        }
        public void showAll() {
            gvAuthor.DataSource = authorService.getAll();
            gvAuthor.DataBind();
        }
        public void ResetControl()
        {
            txtauthorID.Text = "";
            txtauthorname.Text = "";
            txtaddress.Text ="";
            txtPhone.Text = "";
        }
        public void insertAuthor() {
            authorInfo objAuthor = new authorInfo();
            objAuthor.authorName = txtauthorname.Text;
            objAuthor.address = txtaddress.Text;
            objAuthor.phone = txtPhone.Text;
            bool test = authorService.insertAuthor(objAuthor);
            if (test==true)
            {
                Response.Write("<script language='javascript'>alert('insert success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('insert failed');</script>");
            }
        }
        public void updateAuthor()
        {
            authorInfo objAuthor = new authorInfo();
            objAuthor.authorID = Convert.ToInt32(txtauthorID.Text); 
            objAuthor.authorName = txtauthorname.Text;
            objAuthor.address = txtaddress.Text;
            objAuthor.phone = txtPhone.Text;
            bool test = authorService.updateAuthor(objAuthor);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('update success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('update failed');</script>");
            }
        }

        protected void lbtAddT_Click(object sender, EventArgs e)
        {
            flag = true;
            pnUpdate.Visible = true;
            pnView.Visible = false;
            ResetControl();
        }

        protected void lbtRefreshT_Click(object sender, EventArgs e)
        {
            showAll();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            if (flag==true)
            {
                insertAuthor();
            }
            else
            {
                updateAuthor();
            }
            showAll();
            pnView.Visible = true;
            pnUpdate.Visible = false;
        }

        protected void gvAuthor_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int authorID = Convert.ToInt32(gvAuthor.Rows[e.RowIndex].Cells[0].Text);
            if (authorService.checkAuthor(authorID)==true)
            {
                Response.Write("<script language='javascript'>alert('product still existed , dont delete');</script>");
                return;
            }
            bool test = authorService.deleteAuthor(authorID);
            if (test == true)
            {
                Response.Write("<script language='javascript'>alert('delete success');</script>");
            }
            else
            {
                Response.Write("<script language='javascript'>alert('delete failed');</script>");
            }
            showAll();
        }

        protected void gvAuthor_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            flag = false;
            int authorID = Convert.ToInt32(gvAuthor.Rows[e.NewSelectedIndex].Cells[0].Text);
            authorInfo objAuthor = authorService.getbyID(authorID);
            txtauthorID.Text = objAuthor.authorID.ToString();
            txtauthorname.Text = objAuthor.authorName;
            txtaddress.Text = objAuthor.address;
            txtPhone.Text = objAuthor.phone;
            pnUpdate.Visible = true;
            pnView.Visible = false;

        }

        protected void gvAuthor_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAuthor.PageIndex = e.NewPageIndex;
            showAll();
        }
    }
}
